package com.sunchangming.stmdb.helper;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

import junit.framework.Assert;

import org.apache.log4j.BasicConfigurator;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sunchangming.stmdb.TestUtil;

public class NameDirectorySerTest {
	private final static ThreadMXBean tmx = ManagementFactory.getThreadMXBean();

	private final static Logger logger = LoggerFactory.getLogger(NameDirectorySerTest.class);

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		BasicConfigurator.configure();
	}

	private byte[] doWork(Serializer ser, Object obj) throws java.io.IOException {
		byte[] ret = null;
		for (int i = 0; i != 1000; ++i) {
			ret = ser.serialize(obj);
		}
		return ret;
	}

	@SuppressWarnings("unchecked")
	@Test
	public void test() throws java.io.IOException {
		final java.util.Random rand = new java.util.Random();

		final java.util.Map<String, Long> testdata = new java.util.HashMap<String, Long>();
		for (int i = 0; i != 1000; ++i) {
			final String key1 = TestUtil.nextString(10);
			final Long value1 = rand.nextLong();
			testdata.put(key1, value1);
		}

		final NameDirectorySer ser = new NameDirectorySer();
		long time = 0;
		byte[] ret = null;
		for (int i = 0; i != 20; ++i) {
			final long begin = tmx.getCurrentThreadCpuTime();
			ret = doWork(ser, testdata);
			final long end = tmx.getCurrentThreadCpuTime();
			if (i == 180)
				time = end - begin;
		}
		logger.info(testdata.size() + "个entry,序列化之后长度为" + ret.length + ",花费时间=" + time);
		final java.util.Map<String, Long> testdata2 = (java.util.Map<String, Long>) ser.deserialize(ret);
		Assert.assertEquals(testdata, testdata2);
	}

	@Test
	public void test2() throws java.io.IOException {
		final java.util.Random rand = new java.util.Random();

		final java.util.Map<String, Long> testdata = new java.util.HashMap<String, Long>();
		for (int i = 0; i != 1000; ++i) {
			final String key1 = TestUtil.nextString(10);
			final Long value1 = rand.nextLong();
			testdata.put(key1, value1);
		}

		final NameDirectorySer2 ser = new NameDirectorySer2();
		long time = 0;
		byte[] ret = null;
		for (int i = 0; i != 20; ++i) {
			final long begin = tmx.getCurrentThreadCpuTime();
			ret = doWork(ser, testdata);
			final long end = tmx.getCurrentThreadCpuTime();
			if (i == 180)
				time = end - begin;
		}
		logger.info(testdata.size() + "个entry,序列化之后长度为" + ret.length + ",花费时间=" + time);
	}

	@Test
	public void test3() throws java.io.IOException {
		final java.util.Random rand = new java.util.Random();

		final java.util.Map<String, Long> testdata = new java.util.HashMap<String, Long>();
		for (int i = 0; i != 1000; ++i) {
			final String key1 = TestUtil.nextString(10);
			final Long value1 = rand.nextLong();
			testdata.put(key1, value1);
		}

		final NameDirectorySer3 ser = new NameDirectorySer3();
		long time = 0;
		byte[] ret = null;
		for (int i = 0; i != 20; ++i) {
			final long begin = tmx.getCurrentThreadCpuTime();
			ret = doWork(ser, testdata);
			final long end = tmx.getCurrentThreadCpuTime();
			if (i == 180)
				time = end - begin;
		}
		logger.info(testdata.size() + "个entry,序列化之后长度为" + ret.length + ",花费时间=" + time);
	}

}
